package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.WellProduct;
import com.qingxin.park.service.WellProductService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.ui.bean.TreeNode;

/**
 * 名优产品管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/wellProduct")
public class WellProductAction extends BaseAction {
	
	@Autowired
	private WellProductService wellProductService;
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/wellProduct/wellProduct_list";
	}
	
	/**
	 * 加载左侧 园区-名优产品
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<TreeNode> lefttree(String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		
		list.addAll(wellProductService.getUITreeData(getLoginUser(request)));
		return list;
	}
	
	/**
	 * 查询 名优产品列表数据
	 * @param pageParam
	 * @param search
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdataALL(PageParam pageParam,String datatype,String search,String leftdata,HttpServletRequest request){
		return wellProductService.list(pageParam, datatype, search, leftdata, getLoginUser(request), request);
	}
	
	/**
	 * 添加
	 * @param model
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("parkid", leftdata);
		return "/trace/wellProduct/wellProduct_add";
	}
	
	/**
	 * 添加保存
	 * @param wellProduct
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WellProduct wellProduct ,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}		
		SysError info = wellProductService.add(wellProduct,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除
	 * @param productid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] productid, HttpServletRequest request){
		return wellProductService.delete(productid, request);
	}
	
	/**
	 * 修改
	 * @param model
	 * @param articleId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String productid, HttpServletRequest request){
		WellProduct wellProduct = wellProductService.get(productid);
		model.put("wellProduct", wellProduct);
		return "trace/wellProduct/wellProduct_edit";
	}
	
	/**
	 * 保存修改
	 * @param wellProduct
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WellProduct wellProduct, BindingResult result, HttpServletRequest request){
		System.out.println(wellProduct.getCompanyid());
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return wellProductService.update(wellProduct, getLoginUser(request), request);
	}
	
	/** 判断项目名称是否存在
	 * @param itemName
	 * @param itemId
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String isNameDuplicate(String materialName, String repertoryid, String parkid){
		boolean existed = wellProductService.isNameDuplicate(materialName, repertoryid, parkid);
		return existed ? "1":"0";
	}
	
	/**
	 * 企业下拉列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCompanyData", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<Company> getParkData(String parkid, HttpServletRequest request){
		return wellProductService.getParkData(parkid,getLoginUser(request));
	}
	
}
